local table_insert = table.insert
local table_sort = table.sort
local table_remove = table.remove
local math_floor = math.floor
local table_unpack = table.unpack
local math_ceil = math.ceil
--TODO random函数是非线程安全的，这个方法后期要自己实现线程安全的版本
local math_random = math.random
--math.randomseed(os.time())
local util = require "util"
local zjh_rule = require "zhajinhua.zjh_rule"
local constant = require "constant"

local M = {}

--local MAX_PLAYER_NUM = 5
--local MIN_PLAYER_NUM = 2
local MAX_WATCH_PLAYER_NUM = 100 --观战列表
local GAME_REST_TIME = 3 --每局有3秒的休息时间
local PLAY_TIME = 15
local COINS_ANIMATION_TIME = 4
local COMPARE_ANIMATION_TIME = 4

local SIGN_CLUB = 1         --梅花
local SIGN_DIAMOND = 2      --钻石
local SIGN_HEART = 3        --桃心
local SIGN_SPADE = 4        --黑桃

--local GAME_OVER_BY_COMPARE = 1

--[[
    每张卡牌分别由1-10,J,Q,K,black joker,red jocker 定义为1-15
--]]

local GAME_STATUS_INIT = 0          --初始化状态
local GAME_STATUS_STARTED = 1       --游戏已经开始
local GAME_STATUS_OVER = 2          --结束状态
local ZHJ_ANG_MULTIPLE = {1, 3, 5, 8, 10} --暗牌倍数
local ZHJ_MING_MULTIPLE = {2, 6, 10, 16, 20} --明牌倍数

local ZJH = {

}

local function make_card_id(sign,number)
    return sign * 100 + number
end

function ZJH:init()
    --生成所有的牌
    local cards = {}
    for i = 1,13 do
        for j = 1,4 do
            table_insert(cards,make_card_id(j,i))
        end
    end

    self.game_status = GAME_STATUS_INIT
    self.cards = cards
    self.player_cards_map = nil
    self.banker_uid = 0              --庄家uid
    self.next_player_index = 0       --下次出牌人的索引
    self.player_list = {}            --当前游戏中的玩家列表
    self.ming_list = {}              --明牌列表
    self.give_up_list = {}           --放弃列表
    self.fail_list = {}              --失败列表
    self.leave_list = {}             --离开列表
    self.rules = nil                 --规则
    self.next_bet_score = 0          --下家应该下注的一个金额
    self.player_score = {}           --记录玩家的总注金
    self.table_score = {}            --记录桌上的注金单元
    self.run_count = 0               --标记当前进度
    self.run_round = 1               --标记当前的轮数
    self.play_end_time = 0           --下一个出牌玩家的出牌结束时间
    self.compare_list = {}           --比过牌的玩家列表
    self.total_score = 0             --桌面总注额
    -- self.zhj_ang_score = {}
    -- self.zhj_ming_score = {}
    self.auto_bet_list = {}          --自动下注列表
    self.operate_delay_time = nil    --延长下一个玩家出手的时间
    self.winner_uid = 0              
    --self.game_over_reason = false
end

--[[
    判断是否有该玩家了
]]
local function get_player_index(player_list,uid)
    for i,_uid in ipairs(player_list) do
        if _uid == uid then
            return i
        end
    end
end

--[[
    绑定规则
]]
function ZJH:bind_rule(rules)
    self.rules = rules
end

function ZJH:get_player_list()
    return self.player_list
end

--[[
    将玩家加入到列表中
]]
function ZJH:enter(uid)
    if get_player_index(self.player_list,uid) then
        --已经在牌局上了
        return true
    end

    if #self.player_list >= self.rules.play_num then
        return false
    end

    table_insert(self.player_list,uid)
    self.compare_list[uid] = {}
    self.auto_bet_list[uid] = false --默认都是不自动跟注
    --self.next_bet_score = self.zhj_ang_score[1]
    return true
end

--[[
    检查游戏是否可以开始
]]
function ZJH:check()
    -- if #self.player_list < self.rules.least_num then --MIN_PLAYER_NUM then
    --     return false
    -- end

    assert(self.game_status == GAME_STATUS_INIT)
    
    self.game_status = GAME_STATUS_STARTED

    local player_cards_map = {}
    for _,_uid in pairs(self.player_list) do
        player_cards_map[_uid] = {}
    end

    self.player_cards_map = player_cards_map

    return true
end

--[[
    打乱牌
]]
function ZJH:shuffle(swap_times)
    assert(self.game_status == GAME_STATUS_STARTED)
    local cards = self.cards
    
    local card_count = #cards
    swap_times = card_count or swap_times
    for i = 1,swap_times do
        local n = math_random(1,card_count)
        local m = math_random(1,card_count)
        if n ~= m then
            cards[n],cards[m] = cards[m],cards[n]
        end
    end
end

--[[
    发牌 每人三张牌
]]
function ZJH:deal()
    assert(self.game_status == GAME_STATUS_STARTED)
    local cards = self.cards
    local player_cards_map = self.player_cards_map
    assert(cards and player_cards_map)

    local end_index = #self.player_list*zjh_rule.MAX_CARD_COUNT --每人三张牌
    assert(self.player_list ~= 0)
    for i = 1,end_index,#self.player_list do
        local j = 0
        for _,card_id_set in pairs(player_cards_map) do
            table_insert(card_id_set, cards[i + j])
            j = j + 1
        end
    end
    print("deal deal deal" .. tostring_r(player_cards_map))
end

--[[
    获取玩家的牌
]]
function ZJH:get_player_tile_list(uid)
    local player_tile_list = assert(self.player_cards_map[uid])
    return player_tile_list
end

function ZJH:is_ming_pai(uid)
    if self.ming_list[uid] then
        return true
    end

    return false
end

function ZJH:is_fail(uid)
    if not self.fail_list[uid] then
        return false
    end

    return true
end

function ZJH:is_give_up(uid)
    if self.give_up_list[uid] then
        return true
    end

    return false
end

--[[
    获取玩家牌信息
]]
function ZJH:get_player_tile_info()
    local player_tite_info_list = {}
    for _,tmp_uid in pairs(self.player_list) do
        --没有离开游戏的玩家才需要填充数据
        if not self:is_leave(tmp_uid) then
            local player_tile_info = {}
            --明牌过的玩家才需要发送牌
            if self:is_ming_pai(tmp_uid) then
                player_tile_info.uid = tmp_uid
                player_tile_info.tile_list = self:get_player_tile_list(tmp_uid)
            else
                player_tile_info.uid = tmp_uid
                player_tile_info.tile_list = {}
            end
            player_tile_info.cur_bet_score = self:get_player_bet_score(tmp_uid)
            player_tile_info.is_fail = self:is_fail(tmp_uid)
            player_tile_info.is_give_up = self:is_give_up(tmp_uid)
            table_insert(player_tite_info_list,player_tile_info)
        end
    end

    return player_tite_info_list
end

function ZJH:get_banker_uid()
    return self.banker_uid
end

function ZJH:get_table_total_score()
    return self.total_score
end

--[[
    设置庄家
]]
function ZJH:setingbanker_status(_banker_uid)
    assert(self.game_status == GAME_STATUS_STARTED)
    local player_list = self.player_list
    if not _banker_uid then
        self.banker_uid = player_list[math_random(1,#player_list)]
    else
        --查找一下是否存在庄家了
        local have_banker = false
        for _,uid in pairs(player_list) do
            if uid == _banker_uid then
                self.banker_uid = _banker_uid
                have_banker = true
                break
            end
        end

        if not have_banker then
            self.banker_uid = player_list[math_random(1,#player_list)]
        end
    end

    --轮到庄家的下一位出手
    self:trun_banker_next_player()
end

--[[
    是否轮到自己的
]]
function ZJH:is_my_turn(uid)
    return self.next_player_index == get_player_index(self.player_list,uid)
end

function ZJH:set_operate_delay_time()
    self.operate_delay_time = util.get_now_time() + COMPARE_ANIMATION_TIME
end

function ZJH:get_operate_delay_time()
    return self.operate_delay_time
end

function ZJH:clear_operate_delay_time()
    if self.operate_delay_time then
        self.operate_delay_time = nil
    end
end

--[[
    庄家下一位
]]
function ZJH:trun_banker_next_player()
    local next_player_index = get_player_index(self.player_list,self.banker_uid) + 1
    if next_player_index > #self.player_list then
        next_player_index = next_player_index % #self.player_list
    end
    self.next_player_index = next_player_index
    self.play_end_time = util.get_now_time() + PLAY_TIME
end

--[[
    增加游戏进度
]]
function ZJH:add_run_count()
    self.run_count = self.run_count + 1
    if self.run_count == self.rules.play_num then
        self.run_round = self.run_round + 1
        print("new round new round" .. self.run_round)
        self.run_count = 0
    end
end

function ZJH:get_run_round()
    return self.run_round
end

--[[
    轮到下一位操作
]]
function ZJH:turn_next_player()
    local next_player_index = self.next_player_index
    local tmp_next_player_index
    for index = 1, self.rules.play_num, 1 do
        tmp_next_player_index = (next_player_index + index) % self.rules.play_num
        if tmp_next_player_index == 0 then
            tmp_next_player_index = self.rules.play_num
        end

        if self:is_started() then
            self:add_run_count()
        end

        if self.player_list[tmp_next_player_index] then
            if self:player_isvalid(self.player_list[tmp_next_player_index]) then
                self.next_player_index = tmp_next_player_index
                --判断切换的时候需要加上比牌时间不
                if self:get_operate_delay_time() then
                    self.play_end_time = util.get_now_time() + PLAY_TIME + COMPARE_ANIMATION_TIME
                else
                    self.play_end_time = util.get_now_time() + PLAY_TIME
                end
                break
            end 
        end
    end
end

function ZJH:reset_play_end_time()
    if self:get_operate_delay_time() then
        self.play_end_time = util.get_now_time() + PLAY_TIME + COMPARE_ANIMATION_TIME
    else
        self.play_end_time = util.get_now_time() + PLAY_TIME
    end
end

--[[
    获取下一个操作的uid
]]
function ZJH:get_next_player_uid()
    return self.player_list[self.next_player_index] or 0
end

--[[
    判断是否已经看过牌了
]]
function ZJH:have_look_card(uid)
    return self.ming_list[uid]
end

function ZJH:can_look(uid)
    if self:is_game_over() then
        errlog(uid, 'ZJH:look_card game is over')
        return false
    end

    if not get_player_index(self.player_list,uid) then
        errlog(uid, 'ZJH:look_card no in game')
        return false
    end

    if not self:player_isvalid(uid) then
        errlog(uid, 'ZJH:can_look player is not valid')
        return false
    end

    --如果已经明牌过了
    if self:is_ming_pai(uid) then
        return false
    end

    if self.run_round < self.rules.max_look_round then
        return false
    end
    return true
end

--[[
    查看牌操作
]]
function ZJH:look_card(uid)
    self.ming_list[uid] = true
    local src_tile_list = self:get_player_tile_list(uid)
    local card_type = zjh_rule.get_card_type(src_tile_list)
    return card_type, src_tile_list
end

--[[
    判断某个玩家是否还有效
]]
function ZJH:player_isvalid(uid)
    if self.give_up_list[uid] or self.fail_list[uid] or self.leave_list[uid] then
        return false
    end

    return true
end

function ZJH:is_leave(uid)
    if not self.leave_list[uid] then
        return false
    end
    return true
end

--[[
    判断场上还剩多少人
]]
function ZJH:get_left_num()
    local count = 0
    for _,_uid in pairs(self.player_list) do
        if self:player_isvalid(_uid) then
            count = count + 1
        end
    end

    return count
end

function ZJH:can_give_up(uid)
    --检查游戏是否已经结束了
    if self:is_game_over() then
        errlog(uid, 'CZJH:give_up game is over')
        return false
    end

    if not get_player_index(self.player_list,uid) then
        errlog(uid, 'CZJH:look_card no in game')
        return false
    end

    --判断该玩家是否还有效
    if not self:player_isvalid(uid) then
        errlog(uid, 'CZJH:give_up player is not valid')
        return false
    end

    return true
end

--[[
    放弃操作
]]
function ZJH:give_up(uid)
    --标记一下放弃
    self.give_up_list[uid] = true

    if not self:is_game_over() then
        if self:is_my_turn(uid) then
            return true
        end
    end
    return false
end

function ZJH:is_winner(uid)
    return uid == self.winner_uid
end

--[[
    通知当前局有人离开了
]]
function ZJH:leave(uid)
    if not get_player_index(self.player_list,uid) then
        dbglog(uid, 'ZJH:leave no in game')
        return false
    end

    --判断该玩家是否还有效
    if self:is_leave(uid) then
        dbglog(uid, 'ZJH:leave player is leave')
        return false
    end

    --标记一下离开
    self.leave_list[uid] = true

    if not self:is_game_over() then
        if self:is_my_turn(uid) then
            return true
        end
    end

    return false
end

function ZJH:get_next_compare_player_uid()
    --local old_next_player_index = self.next_player_index
    local next_player_index = self.next_player_index
    local tmp_next_player_index
    --local find = false
    for index = 1, self.rules.play_num, 1 do
        tmp_next_player_index = (next_player_index + index) % self.rules.play_num
        if tmp_next_player_index == 0 then
            tmp_next_player_index = self.rules.play_num
        end

        if self.player_list[tmp_next_player_index] then
            if self:player_isvalid(self.player_list[tmp_next_player_index]) and self.next_player_index ~= tmp_next_player_index then
                return self.player_list[tmp_next_player_index]
            end
        end
    end

    return 0
end

function ZJH:set_winner()
    for _,_uid in pairs(self.player_list) do
        if self:player_isvalid(_uid) then
            self.winner_uid = _uid
            break
        end
    end
end

function ZJH:is_game_over()
    if self.game_status == GAME_STATUS_OVER then
        return true
    end
    local count = self:get_left_num()
    if count >= 2 then
        return false
    end
    self:set_winner()
    self.game_status = GAME_STATUS_OVER
    return true
end


function ZJH:is_started()
    return self.game_status == GAME_STATUS_STARTED
end

function ZJH:add_player_bet_score(uid,score)
    if not self.player_score[uid] then
        self.player_score[uid] = score
    else
        self.player_score[uid] = self.player_score[uid] + score
    end
end

function ZJH:get_player_bet_score(uid)
    if not self.player_score[uid] then
        return 0
    end
    return self.player_score[uid]
end

function ZJH:add_table_score(score)
    table_insert(self.table_score,score)
    self.total_score = self.total_score + score
end

--[[
    开局下的底注
]]
function ZJH:bet_begin_score()
    if self:is_game_over() then
        dbglog('ZJH:bet_begin_score game is over')
        return
    end

    self.next_bet_score = self.rules.dizhu


    for _, uid in pairs(self.player_list) do
        self:add_player_bet_score(uid,self.rules.dizhu)
        self:add_table_score(self.rules.dizhu)
    end
end

function ZJH:get_can_bet_score(uid)
    if self:is_ming_pai(uid) then
        return self.next_bet_score * 2
    end
    return self.next_bet_score
end

function ZJH:get_ding_zhu(uid)
    if self:is_ming_pai(uid) then
        return self.rules.dingzhu * 2
    end
    return self.rules.dingzhu
end

function ZJH:get_add_bet_info(uid)
    local dingzhu = self:get_ding_zhu(uid)
    local scale = 1
    for _, bet_range in pairs(constant.ZJH_ADD_BET_RANGE) do
        if dingzhu >= bet_range[1] and dingzhu <= bet_range[2] then
            scale = bet_range[3]
            break
        end
    end
    local bet_score = self.next_bet_score
    if self:is_ming_pai(uid) then
        bet_score = bet_score*2 + scale
    else
        bet_score = bet_score + scale
    end

    return bet_score,dingzhu,scale
end

function ZJH:score_isvalid(uid,score)
    --检测下注范围是否落在有效范围内
    local bet_score = self:get_can_bet_score(uid)
    local min_add_bet_score,max_add_bet_score,scale = self:get_add_bet_info(uid)
    if score < bet_score or score > max_add_bet_score then
        errlog(uid, 'ZJH:score_isvalid score <= bet_score or score > max_add_bet_score',score,bet_score,max_add_bet_score)
        return false
    end

    -- if score == bet_score or (score - min_add_bet_score)%scale == 0 then
    --     return true
    -- end

    --errlog(uid, 'score ~= bet_score or (score - min_add_bet_score)/scale ~= 0',score,bet_score,min_add_bet_score)
    return true
end

--[[
    请求加注
]]
function ZJH:add_score(uid,score)    
    if self:is_game_over() then
        errlog(uid, 'ZJH:add_score game is over')
        return false
    end

    if not get_player_index(self.player_list,uid) then
        errlog(uid, 'ZJH:add_score no in game')
        return false
    end

    --检测是不是轮到我出手
    if not self:is_my_turn(uid) then
        errlog(uid, 'ZJH:add_score no is my trun')
        return false
    end

    if not self:player_isvalid(uid) then
        errlog(uid, 'ZJH:add_score failed because player is no valid')
        return false
    end

    --正常下注才需要判断当前进行的轮数大于最大可比轮数
    if score then
        --判断轮数是否达到可比轮了
        if self.run_round >= self.rules.comparable_bet_round then
            errlog(uid, 'ZJH:add_score self.run_round >= rules.comparable_bet_round')
            return false
        end

        --判断当前玩家下注的金额数是否合法
        if not self:score_isvalid(uid,score) then
            errlog(uid, 'ZJH:add_score not self:score_isvalid(uid,score)')
            return false
        end

    else
        score = self:get_can_bet_score(uid)
        if self.run_round >= self.rules.comparable_bet_round then
            score = 0
        end
    end

    self:add_player_bet_score(uid,score)
    self:add_table_score(score)

    --保存当前家的下注积分
    if self:is_ming_pai(uid) then
        self.next_bet_score = math_ceil(score / 2)
    else
        self.next_bet_score = score
    end

    return true, score
end

--[[ 
    加入到失败列表中
]]
function ZJH:add_fail_list(uid)
    self.fail_list[uid] = true
end

--[[
    加入到比牌列表中
]]
function ZJH:add_compare_list(uid,dst_uid)
    table_insert(self.compare_list[uid],dst_uid)
end

function ZJH:is_can_compare(uid)
    if self:is_game_over() then
        return false
    end

    if self.run_round < self.rules.max_bet_round then
        return false
    end

    return true
end

function ZJH:can_compare(uid,dst_uid)
    if self:is_game_over() then
        errlog(uid, 'ZJH:compare_card game is over')
        return false
    end

    --检测是不是轮到我出手
    if not self:is_my_turn(uid) then
        errlog(uid, 'ZJH:compare_card no is my trun')
        return false
    end

    if not get_player_index(self.player_list,uid) then
        errlog(uid, 'ZJH:compare_card no in game')
        return false
    end

    if not get_player_index(self.player_list,dst_uid) then
        errlog(dst_uid, 'ZJH:compare_card no in game')
        return false
    end

    if not self:player_isvalid(uid) then
        errlog(uid, 'ZJH:can_look player is not valid')
        return false
    end

    if not self:player_isvalid(dst_uid) then
        errlog(dst_uid, 'ZJH:compare_card player is not valid')
        return false
    end

    if self.run_round < self.rules.max_bet_round then
        errlog(uid, 'ZJH:compare_card can not compare self.run_round < self.rules.max_bet_round')
        return false
    end

    return true
end

--[[
    比牌
]]
function ZJH:compare(uid,dst_uid)
    --加入到比牌列表中
    self:add_compare_list(uid,dst_uid)
    self:add_compare_list(dst_uid,uid)

    local src_tile_list = self:get_player_tile_list(uid)
    local dst_tile_list = self:get_player_tile_list(dst_uid)
    local src_type = zjh_rule.get_card_type(src_tile_list)
    local dst_type = zjh_rule.get_card_type(dst_tile_list)
    local win = zjh_rule.compare_card(src_type,src_tile_list,dst_type,dst_tile_list)

    --加入到失败列表中
    if win then
        self:add_fail_list(dst_uid)
    else
        self:add_fail_list(uid)
    end

    local is_trun_next = false
    if not self:is_game_over() then
        if self:is_my_turn(uid) then
            is_trun_next = true
        end
    end

    return win,is_trun_next,src_type,dst_type
end

function ZJH:get_play_end_time()
    return self.play_end_time
end

function ZJH:get_card_type(uid)
    local tile_list = self:get_player_tile_list(uid)
    return zjh_rule.get_card_type(tile_list)
end

function ZJH:get_game_result()
    if not self:is_game_over() then
        return false
    end

    if not self.player_score then
        return false
    end

    local winners = {}
    local losers = {}
    for uid, score in pairs(self.player_score) do
        local player_record = {}
        player_record.uid = uid

        --注意 此时有可能在结算期间 最后一个玩家也离开了桌子 那么此时这种情况即最后一个玩家获胜
        if not self:player_isvalid(uid) and not self:is_winner(uid) then
            player_record.add_score = -score
        else
            --胜利的人获取所有积分
            player_record.add_score = self:get_table_total_score() - score
        end

        if player_record.add_score > 0 then
            local tile_list = self:get_player_tile_list(uid)
            player_record.card_type = zjh_rule.get_card_type(tile_list)
            table_insert(winners, player_record)
        else
            table_insert(losers, player_record)
        end
    end

    return true, {winners = winners,losers = losers}
end

function ZJH:get_compare_card_list(uid)
    local player_card_list = {}
    for _, dst_uid in pairs(self.compare_list[uid]) do
        --跟我比牌的人没有离开游戏那么久可以看到对方的牌
        if not self:is_leave(dst_uid) then
            local player_card = {}
            player_card.uid = dst_uid
            player_card.tile_list = self:get_player_tile_list(dst_uid)
            player_card.card_type = zjh_rule.get_card_type(player_card.tile_list)
            table_insert(player_card_list, player_card)
        end
    end

    local player_card = {}
    player_card.uid = uid
    player_card.tile_list = self:get_player_tile_list(uid)
    player_card.card_type = zjh_rule.get_card_type(player_card.tile_list)
    table_insert(player_card_list, player_card)

    return player_card_list
end

function ZJH:get_buttons_status(uid)
    local player_buttons_status = {}
    if get_player_index(self.player_list,uid) and self:player_isvalid(uid) then
        player_buttons_status.can_look = self:can_look(uid)
        if not self:is_my_turn(uid) then
            player_buttons_status.can_compare = false
            player_buttons_status.can_bet = false
            player_buttons_status.can_bet = false
            player_buttons_status.can_continue_bet = true
            player_buttons_status.is_auto_bet = self:is_auto_bet(uid)
        else
            if self.run_round >= self.rules.comparable_bet_round then
                player_buttons_status.can_compare = false
                player_buttons_status.can_bet = false
                player_buttons_status.can_continue_bet = false
                player_buttons_status.is_auto_bet = false
                player_buttons_status.force_compare_uid = self:get_next_compare_player_uid()
            else 
                player_buttons_status.can_compare = self:is_can_compare(uid)
                player_buttons_status.bet_score = self:get_can_bet_score(uid)
                local min_add_bet_score,max_add_bet_score,scale = self:get_add_bet_info(uid)
                player_buttons_status.min_add_bet_score = min_add_bet_score
                player_buttons_status.max_add_bet_score = max_add_bet_score
                player_buttons_status.scale = scale
                if player_buttons_status.min_add_bet_score > player_buttons_status.max_add_bet_score then
                    player_buttons_status.can_bet = false
                    player_buttons_status.min_add_bet_score = player_buttons_status.max_add_bet_score
                else
                    player_buttons_status.can_bet = true
                end
                player_buttons_status.can_continue_bet = true
                player_buttons_status.is_auto_bet = self:is_auto_bet(uid)
                --自动跟注状态下不能比牌和不能加注了
                if player_buttons_status.is_auto_bet then
                    player_buttons_status.can_bet = false
                    player_buttons_status.can_compare = false
                end
            end
        end
    end

    return player_buttons_status
end

--[[
    获取游戏当前状态信息
]]
function ZJH:get_game_status(uid)
    local game_status = {}
    game_status.banker_uid = self:get_banker_uid()
    game_status.cur_status = self.game_status
    game_status.player_tile_info_list = self:get_player_tile_info()
    game_status.next_player_uid = self:get_next_player_uid()
    game_status.bet_list = self.table_score
    game_status.run_cycle = self.run_round
    local curr_time = util.get_now_time() 
    local play_end_time = self:get_play_end_time()
    if play_end_time > curr_time then
        game_status.next_player_operate_time = play_end_time - curr_time
    end
    game_status.total_score = self:get_table_total_score()
    game_status.buttons_status = self:get_buttons_status(uid)
    return game_status
end

function ZJH:set_auto_bet(uid,is_auto)
    if self:is_game_over() then
        errlog(uid, 'CZJH:set_auto_bet game is over')
        return false
    end

    if not get_player_index(self.player_list,uid) then
        errlog(uid, 'CZJH:set_auto_bet no in game')
        return false
    end

    if not self:player_isvalid(uid) then
        errlog(uid, 'CZJH:set_auto_bet player is not valid')
        return false
    end

    if is_auto then
        self.auto_bet_list[uid] = true
    else
        self.auto_bet_list[uid] = false
    end
end

function ZJH:is_auto_bet(uid)
    return self.auto_bet_list[uid]
end

-- function ZJH:is_game_over_by_compare()
--     return self.game_over_reason == GAME_OVER_BY_COMPARE
-- end

--M.MAX_PLAYER_NUM         = MAX_PLAYER_NUM
--M.MIN_PLAYER_NUM         = MIN_PLAYER_NUM
M.MAX_WATCH_PLAYER_NUM   = MAX_WATCH_PLAYER_NUM
M.GAME_REST_TIME         = GAME_REST_TIME
M.COINS_ANIMATION_TIME   = COINS_ANIMATION_TIME
M.COMPARE_ANIMATION_TIME = COMPARE_ANIMATION_TIME
M.PLAY_TIME              = PLAY_TIME


function M.new()
    local o = {}
    return setmetatable(o,{__index = ZJH})
end

return M
